package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.CommentService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Integer saveComment(Comment comment) {
        // 1.保存comment对象
        mongoTemplate.save(comment);
        // 2.获取操作类型
        if (comment.getCommentType() == 1) { // 动态点赞
            // 进行数量+1
            Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
            movement.setLikeCount(movement.getLikeCount() + 1);
            mongoTemplate.save(movement);
            // 返回点赞数量
            return movement.getLikeCount();
        } else if (comment.getCommentType() == 2) { // 动态评论
            // 进行数量+1
            Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
            movement.setCommentCount(movement.getCommentCount() + 1);
            mongoTemplate.save(movement);
            // 返回点赞数量
            return movement.getCommentCount();
        } else if (comment.getCommentType() == 3) {// 动态喜欢
            // 进行数量+1
            Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
            movement.setLoveCount(movement.getLoveCount() + 1);
            mongoTemplate.save(movement);
            // 返回点赞数量
            return movement.getLoveCount();
        }
        return 0;
    }

    @Override
    public Integer removeComment(String publishId, Long userId, Integer commentType) {
        // 1.删除comment对象
        // 1-1构建查询对象
        Query query = new Query(
                Criteria.where("publishId").is(new ObjectId(publishId))
                        .and("userId").is(userId)
                        .and("commentType").is(commentType)
        );
        // 1-2 进行删除
        mongoTemplate.remove(query, Comment.class);
        // 2.获取操作类型
        if (commentType == 1) { // 动态点赞
            // 进行数量+1
            Movement movement = mongoTemplate.findById(new ObjectId(publishId), Movement.class);
            movement.setLikeCount(movement.getLikeCount() - 1);
            mongoTemplate.save(movement);
            // 返回点赞数量
            return movement.getLikeCount();
        } else if (commentType == 2) { // 动态评论
            // 进行数量+1
            Movement movement = mongoTemplate.findById(new ObjectId(publishId), Movement.class);
            movement.setCommentCount(movement.getCommentCount() - 1);
            mongoTemplate.save(movement);
            // 返回点赞数量
            return movement.getCommentCount();
        } else if (commentType == 3) {// 动态喜欢
            // 进行数量+1
            Movement movement = mongoTemplate.findById(new ObjectId(publishId), Movement.class);
            movement.setLoveCount(movement.getLoveCount() - 1);
            mongoTemplate.save(movement);
            // 返回点赞数量
            return movement.getLoveCount();
        }
        return 0;
    }

    // 查询评论分页数据
    @Override
    public PageBeanVo findCommentByPage(String publishId, Integer commentType, Integer pageNum, Integer pageSize) {
        // 1.构建查询条件
        Integer index = (pageNum - 1) * pageSize;
        Query query = new Query(
                Criteria.where("publishId").is(new ObjectId(publishId))// 发布id
                        .and("commentType").is(commentType)//  操作类型
        ).with(Sort.by(Sort.Order.desc("created"))) // 排序
                .skip(index).limit(pageSize);// 分页
        // 2.查询评论列表
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        // 3.查询总记录数
        long count = mongoTemplate.count(query, Comment.class);
        // 4.返回分页对象
        return new PageBeanVo(pageNum, pageSize, count, commentList);
    }

    // 查询点赞、评论、喜欢的操作人信息
    @Override
    public PageBeanVo findCommentUserByPage(Long userId, Integer commentType, Integer pageNum, Integer pageSize) {
        // 1.组装查询对象
        Query query = new Query(Criteria.where("publishUserId").is(userId).and("commentType").is(commentType))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);
        // 2.查询list
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        // 3.查询总记录数
        long count = mongoTemplate.count(query, Comment.class);
        // 4.返回分页对象
        return new PageBeanVo(pageNum, pageSize, count, commentList);
    }
}
